<?php
/**
* WDC Like Dislike
* @author WDC <contact@web-designs-company.com>
* @link http://www.web-designs-company.com/
* @copyright (C) 2011 - 2012 JExtension
* @license GNU/GPL, see LICENSE.php for full license.
**/

// no direct access
// Check to ensure this file is included in Joomla!
defined( '_JEXEC' ) or die();
$debug	= 0;

jimport('joomla.plugin.plugin');
JPlugin::loadLanguage('plg_content_vote');

$db		= & JFactory::getDBO();
$id		= @ JRequest::getVar('id');
$ip		= @ JRequest::getVar('ip');

$mainframe->registerEvent('onPrepareContent', 'plgContentvotes');

function plgcontentvotes( &$article, &$params, $limitstart )
{
	global $mainframe;

	$regex			= '/{(cvote)\s*(.*?)/i';
	$plugin			= & JPluginHelper::getPlugin('content', 'content_vote');

	// to get any params for the plugin
	$pluginParams	= new JParameter($plugin->params);

	//get global params from config
	$parms			= array();
	$matches		= array();

	preg_match_all($regex, $article->text, $matches, PREG_SET_ORDER);

	$db				= & JFactory::getDBO();
	$option			= "com_wdclikedislike";
	$sel			= "SELECT `params` FROM #__components WHERE `option`='$option'";
	$db->setQuery($sel);
	$opt			= $db->loadResult();
	$art_idd 		= explode("=",$opt);

	$arts_id		= @$art_idd[3];

	if($opt!=""){
		foreach ($matches as $elm) {
			$id			= JRequest::getVar('id');
			$match_code	= str_replace("id=$id", " ", $opt);
 	 		$opt_code	= str_replace(" ", "_",trim($match_code));
 	 		$opt_codes	= str_replace(" ", "_",trim($match_code));
 	 	 	$opt_code	= str_replace("dlike", "&dlike",$opt_code);
		 	$code		= "{cvote ";
			$code1		= "}";
	 	 	$final_code	= $code.$opt_codes;
			$line		= str_replace("&nbsp;", " ", $elm[2]);
			$keywords	= $elm[0];

			$select		= "SELECT introtext FROM #__content WHERE id=$arts_id";
			$db->setQuery($select);
			$text		= $db->loadResult();
			$regex 		= '/{(cvote)\s*(.*?)}/i';
			$matches 	= array();

			preg_match_all($regex, $text, $matches, PREG_SET_ORDER);

			foreach ($matches as $elm) {
				if($elm[2] != ""){
					$line		= str_replace("&nbsp;", " ", $elm[2]);
				  	$replaces	= "update #__content set introtext = replace(introtext,'$line',' ') where id='$arts_id'";
		 		 	$db->setQuery($replaces);
				 	$db->query();
				}
			}

		  	$replace		= "update #__content set introtext = replace(introtext,'$keywords','$final_code') where id='$arts_id'";
 		 	$db->setQuery($replace);
 		 	$db->query();

			$html			= getvote($parms,$pluginParams,1);
			$article->text	= preg_replace($regex, $html, $article->text, 1);

		   $replace_params	= "UPDATE #__components SET `params` = '' WHERE `option`='com_wdclikedislike'";
	 	   $db->setQuery($replace_params);
		   $db->query();
		}

		$doc	= & JFactory::getDocument();
		$alert="alert('If you declared the variables via Params,Please go to particular article ,refresh it and save,')";
		$doc->addScriptDeclaration($alert);
	 } else {
		$regex 			= '/{(cvote)\s*(.*?)}/i';
		$plugin			= & JPluginHelper::getPlugin('content', 'content_vote');

		// to get any params for the plugin
			$pluginParams	= new JParameter( $plugin->params );

		//get global params from config
			$parms			= array();
			$matches		= array();

			preg_match_all($regex, $article->text, $matches, PREG_SET_ORDER);

		foreach ($matches as $elm) {
		 	$line=str_replace("&nbsp;", " ", $elm[2]);
			$line=str_replace(" ", "&", $line);
			$line=str_replace('"', " ",$line);
			parse_str( $line,$parms );

			$html=getvote($parms,$pluginParams,1);
			$article->text = preg_replace($regex, $html, $article->text, 1);
		}
	 }
	return true;
}
?>

<script type="text/javascript" src="<?php echo JURI::root().'plugins/content/wdc_assets/accordionview.js'?>"></script>
<script type="text/javascript">
	var menu1 = new YAHOO.widget.AccordionView('<?php echo $template; ?>', {collapsible: true, width: '100%', margin : '0', animationSpeed: '0.3', animate: true, effect: YAHOO.util.Easing.easeBothStrong});
</script>

<?php
function getvote($params, $pluginParams, $debugmode=0)
{
	global $Itemid, $mainframe;

	require_once('plugins/content/wdc_assets/cvote_sql.php');

 	$lnews		= $params['like'];
 	$lnews		= str_replace("_"," ",$lnews);
 	$dnews		= $params['dlike'];
	$dnews		= str_replace("_"," ",$dnews);

	$like		= 'plugins/content/wdc_assets/cimages/like.png';
	$dlike		= 'plugins/content/wdc_assets/cimages/dislike.png';
	$like_btm	= 'plugins/content/wdc_assets/cimages/dropdown_bottom1.png';
	$dlike_btm	= 'plugins/content/wdc_assets/cimages/dropdown_bottom2.png';

	$doc		= & JFactory::getDocument();
	$document	= & JFactory::getDocument();
	$document->addStyleSheet('plugins/content/wdc_assets/style.css');

	 $js1="
			var timeout		= 500;
			var closetimer	= 0;
			var ddmenuitem	= 0;

			// open hidden layer
			function mopen(id)
			{
				// cancel close timer
				mcancelclosetime();

				// close old layer
				if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';

				// get new layer and show it
				ddmenuitem = document.getElementById(id);
				ddmenuitem.style.visibility = 'visible'; //ddmenuitem.style.position = 'relative';

			}
			// close showed layer
			function mclose(id)
			{
				if(ddmenuitem) ddmenuitem.style.visibility = 'hidden'; //ddmenuitem.style.position = 'absolute';
			}

			// go close timer
			function mclosetime()
			{
				closetimer = window.setTimeout(mclose, timeout);
			}

			// cancel close timer
			function mcancelclosetime()
			{
				if(closetimer)
				{
					window.clearTimeout(closetimer);
					closetimer = null;
				}
			}

			// close layer when click-out
			document.onclick = mclose; ";

		$doc->addScriptDeclaration($js1);
		$db					= &JFactory::getDBO();
	 	$id					= JRequest::getVar('id');

		include(JPATH_ROOT.DS."components".DS."com_wdclikedislike".DS."models".DS."wdclikedislike.php");
		$get_model			= new wdclikedislikeModelwdclikedislike();
		$like_result		= $get_model->getResponsecount_like($id);
		$dlike_result		= $get_model->getResponsecount_dlike($id);

		if ($like_result == ''){
			  $like_result = 0;
		}

		if ($dlike_result == ''){
			$dlike_result = 0;
		}

		$my		= "function myalert(a){var x= document.getElementById('myHeader').style.display='block';}";
		$doc->addScriptDeclaration($my);
		$db		= &JFactory::getDBO();

		$html  = "";

		$html .= "<div class='main_cont'>";
		$html .= "\n<form name='Content_vote' action='' method='post'>";
		$c	   = explode(",",$lnews);
		$html .= "<table width='100%' id='wdc'><tr><td class='top'>&nbsp;</td></tr>";
		$html .= "<tr><td class='middle'>";
	 	$html .= "<div id='like'><ul id='sddm' style='margin:0px;'>";
		$html .= "<li style='padding:0px;margin:10px 0 0;'><a href='#'  onmouseover=mopen('m1') onmouseout=mclosetime()><img class='imgclass' src='$like'></a>";
		$html .= "<div id='m1' onmouseover=mcancelclosetime() onmouseout=mclosetime()>";

	   for($i=0;$i<count($c);$i++){
		   	$ar				= $c[$i];$id=$_GET['id'];
		   	$arr_num		= $i;
		   	$arr_num		= $ar;
		   	$count			= count($c);
		   	$ip				= $_SERVER['REMOTE_ADDR'];
			$takeip			= "SELECT ip FROM #__cvote WHERE ip='$ip' AND article_id='$id'";
			$db->setQuery($takeip);
			$ips			= $db->loadResult();
			if($ip==$ips){
				$html 		.=	"<input type=\"hidden\" name=\"je_already_added_like\" id=\"je_already_added_like\" value=\" 0 \" />";

			}
			$onclick_yes	= "onclick=\"getResponse('$i','$id',1,'$count','1','$ip');\"";
			$html 			.="<a href='javascript:void(0)' id='like$i' $onclick_yes >$ar</a>";

	   }

	$path	= JURI::root();

	$html .="<input type=\"hidden\" name=\"path\" id=\"path\" value=\"$path\" />";
	$html .="<img style='margin:0px;float:left;' src='$like_btm'>";
	$html .="</div></li><div id='m1' onmouseover=mcancelclosetime() onmouseout=mclosetime()>";
	$select="SELECT COUNT( * )FROM `#__cvote` WHERE `like` = '1' AND `article_id`='$id'";
	$db->setQuery($select);$l_result=$db->loadResult();
	$html .="</ul>";
	$html .="<ul id='countid' style='margin: 22px 0 0;text-align: left;'><li class='like_count' style='color: #000000;padding:0px;'><span id=\"je-responselike\" ><b>$like_result</b></span><div id='already_like' style='display:none;' class='already_voted'>Already Voted</div></li></ul>";
  	$html .="</li></ul></div>";
	$html .="<div id='dlike'><ul id='sddm1' style='padding: 4px 5px;margin:0px;'>";
	$html .="<li style='padding:0px;margin:0px;'><a href='#' onmouseover=mopen('m2') onmouseout=mclosetime()><img class='imgclass' src='$dlike'></a>";
	$c=explode(",",$dnews);
	$html .="<div id='m2' onmouseover=mcancelclosetime() onmouseout=mclosetime()>";

	for($i=0;$i<count($c);$i++){
	   	$ar		= $c[$i];
		$ip		= $_SERVER['REMOTE_ADDR'];
		$takeip	= "SELECT ip FROM #__cvote WHERE ip='$ip' AND article_id='$id'";
		$db->setQuery($takeip);
		$ips	= $db->loadResult();
		if($ip==$ips){
			$html 		.=	"<input type=\"hidden\" name=\"je_already_added_dlike\" id=\"je_already_added_dlike\" value=\" 0 \" />";

		}
		$onclick_yes = "onclick=\"getResponse('$i','$id',1,'$count','2','$ip');\"";
		$html .="<a  href='javascript:void(0)' id='dlike$i' $onclick_yes>$ar</a>";

	}

	$html 		.="<img style='margin:0px;float:left;' src='$dlike_btm'>";
	$html 		.="</div><div id='m2' onmouseover=mcancelclosetime() onmouseout=mclosetime()>";

	$select2	="SELECT COUNT( * )FROM `#__cvote` WHERE `dlike` = '1' AND `article_id`='$id'";
	$db->setQuery($select2);$d_result=$db->loadResult();

	$html		.="</div></ul><ul id='countid' style='margin:0 0 0 6px;color:#000000;padding:0px;'><li class='count' style='padding:0px;margin:0px;'><span id=\"je-responsedlike\" >$dlike_result</span><div id='already_like' style='display:none;' class='already_voted'>Already Voted</div></li></ul></div>";

	$html		.="</td></tr><tr><td class='btm'>&nbsp;<span class='web'><a style='color:#000000;text-decoration: none;' href='http://www.web-designs-company.com/' target='_blank'>Website Design Company</a></span></td></tr></table></form></div>";

	$html		.="";

	return $html;
}
?>